Certified Java Programmer Mock Exam


Question 1


1. class A {
2.   void m1() {throw new ArithmeticException();}
3.   void m2() {throw new ClassCastException();}
4.   void m3() {throw new IllegalArgumentException();}
5.   void m4() {throw new IndexOutOfBoundsException();}
6.   void m5() {throw new NullPointerException();}
7.   void m6() {throw new SecurityException();}
8. }

What is the result of attempting to compile the program?

a. Compiler error at line 2.
b. Compiler error at line 3.
c. Compiler error at line 4.
d. Compiler error at line 5.
e. Compiler error at line 6.
f. Compiler error at line 7.
g. None of the Above

Question 2


class Level1Exception extends Exception {}
class Level2Exception extends Level1Exception {}
class Level3Exception extends Level2Exception {}
class Purple {
  public static void main(String args[]) {
    int a,b,c,d,f,g,x;
    a = b = c = d = f = g = 0;
    x = 3;
    try {
      try {
        switch (x) {
          case 1: throw new Level1Exception();
          case 2: throw new Level2Exception();
          case 3: throw new Level3Exception();
        }
        a++;
      }
      catch (Level2Exception e) {b++;}
      finally{c++;}
    }
    catch (Level1Exception e) { d++;}
    catch (Exception e) {f++;}
    finally {g++;}
    System.out.print(a+","+b+","+c+","+d+","+f+","+g);
  }
}

What is the result of attempting to compile and run the program?

a. Prints: 1,1,1,0,0,1
b. Prints: 0,1,1,0,0,1
c. Prints: 0,1,0,0,0,0
d. Prints: 0,1,0,0,0,1
e. Prints: 0,0,1,0,0,1
f. Compiler Error
g. Run Time Error
h. None of the Above

Question 3


class A {
  static boolean b;
  public static void main(String[] args) {
    if (b) {
      System.out.print("A");
    } else if (b = false) {
      System.out.print("B");
    } else if (b) {
      System.out.print("C");
    } else if (!b) {
      System.out.print("D");
    } else {
      System.out.print("E");
    }
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: A
b. Prints: B
c. Prints: C
d. Prints: D
e. Prints: E
f. Runtime Exception
g. Compiler Error
h. None of the Above

Question 4


class ColorException extends Exception {}
class WhiteException extends ColorException {}
class White {
  void m1() throws ColorException {throw new ColorException();}
  void m2() throws WhiteException {throw new WhiteException();}
  public static void main (String[] args) {
    White white = new White();
    int a,b,d,f;
    a = b = d = f = 0;
    try {white.m1();a++;} catch (WhiteException e) {b++;}
    try {white.m2();d++;} catch (WhiteException e) {f++;}
    System.out.print(a+","+b+","+d+","+f);
  }
}

What is the result of attempting to compile and run the program?

a. Prints: 0,1,0,0
b. Prints: 1,1,0,1
c. Prints: 0,1,0,1
d. Prints: 0,1,1,1
e. Prints: 1,1,1,1
f. Compiler Error
g. Run Time Error
h. None of the Above

Question 5


class ColorException extends Exception {}
class WhiteException extends ColorException {}
abstract class Color {
  abstract void m1() throws ColorException;
}
class White extends Color {
  void m1() throws WhiteException {throw new WhiteException();}
  public static void main (String[] args) {
    White white = new White();
    int a,b,c,d;
    a = b = c = 0;
    try {
      white.m1();
      a++;
    } catch (WhiteException e) {b++;}
    finally {c++;}
    System.out.print(a+","+b+","+c);
  }
}

What is the result of attempting to compile and run the program?

a. Prints: 0,0,0
b. Prints: 0,0,1
c. Prints: 0,1,0
d. Prints: 0,1,1
e. Prints: 1,0,0
f. Prints: 1,0,1
g. Prints: 1,1,0
h. Prints: 1,1,1
i. Compiler Error
j. Run Time Error
k. None of the Above

Question 6


class Level1Exception extends Exception {}
class Level2Exception extends Level1Exception {}
class Level3Exception extends Level2Exception {}
class Purple {
  public static void main(String args[]) {
    int a,b,c,d,f,g,x;
    a = b = c = d = f = g = 0;
    x = 4;
    try {
      try {
	switch (x) {
          case 1: throw new Level1Exception();
          case 2: throw new Level2Exception();
          case 3: throw new Level3Exception();
          case 4: throw new Exception();
        }
        a++;
      }
      catch (Level2Exception e) {b++;}
      finally{c++;}
    }
    catch (Level1Exception e) { d++;}
    catch (Exception e) {f++;}
    finally {g++;}
    System.out.print(a+","+b+","+c+","+d+","+f+","+g);
  }
}

What is the result of attempting to compile and run the program?

a. Prints: 0,0,0,0,0,1
b. Prints: 0,0,0,0,1,0
c. Prints: 0,0,1,0,0,1
d. Prints: 0,0,1,0,1,1
e. Prints: 0,1,1,1,1,1
f. Prints: 1,1,1,1,1,1
g. Compiler Error
h. Run Time Error
i. None of the Above

Question 7


class A {
  public static void main(String[] args) {
    boolean b = true;
    if (b = false) {
      System.out.print("A");
    } else if (b) {
      System.out.print("B");
    } else {
      System.out.print("C");
    }
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: A
b. Prints: B
c. Prints: C
d. Runtime Exception
e. Compiler Error
f. None of the Above

Question 8


class ColorException extends Exception {}
class WhiteException extends ColorException {}
class White {
  void m1() throws ColorException {throw new ColorException();}
  void m2() throws WhiteException {throw new ColorException();}
  public static void main (String[] args) {
    White white = new White();
    int a,b,d,f;
    a = b = d = f =0;
    try {white.m1();a++;} catch (ColorException e) {b++;}
    try {white.m2();d++;} catch (WhiteException e) {f++;}
    System.out.print(a+","+b+","+d+","+f);
  }
}

What is the result of attempting to compile and run the program?

a. Prints: 0,1,0,0
b. Prints: 1,1,0,1
c. Prints: 0,1,0,1
d. Prints: 0,1,1,1
e. Prints: 1,1,1,1
f. Compiler Error
g. Run Time Error
h. None of the Above

Question 9


class Level1Exception extends Exception {}
class Level2Exception extends Level1Exception {}
class Level3Exception extends Level2Exception {}
class Brown {
  public static void main(String args[]) {
    int a, b, c, d, f;
    a = b = c = d = f = 0;
    int x = 2;

    try {
      switch (x) {
        case 1: throw new Level1Exception();
        case 2: throw new Level2Exception();
        case 3: throw new Level3Exception();
      }
      a++;
    }
    catch (Level3Exception e) { b++;}
    catch (Level2Exception e) { c++;}
    catch (Level1Exception e) { d++;}
    finally {f++;}
    System.out.print(a+","+b+","+c+","+d+","+f);
  }
}

What is the result of attempting to compile and run the program?

a. Prints: 0,0,0,1,1
b. Prints: 0,0,1,1,1
c. Prints: 0,1,1,1,1
d. Prints: 1,1,1,1,1
e. Prints: 0,0,1,0,1
f. Prints: 0,1,0,0,1
g. Prints: 1,0,0,0,1
h. Compiler Error
i. Run Time Error
j. None of the Above

Question 10


class Level1Exception extends Exception {}
class Level2Exception extends Level1Exception {}
class Level3Exception extends Level2Exception {}
class Purple {
  public static void main(String args[]) {
    int a,b,c,d,f,g,x;
    a = b = c = d = f = g = 0;
    x = 2;
    try {
      try {
        switch (x) {
          case 1: throw new Level1Exception();
          case 2: throw new Level2Exception();
          case 3: throw new Level3Exception();
        }
        a++;
      }
      catch (Level2Exception e) {b++;}
      finally{c++;}
    }
    catch (Level1Exception e) { d++;}
    catch (Exception e) {f++;}
    finally {g++;}
    System.out.print(a+","+b+","+c+","+d+","+f+","+g);
  }
}

What is the result of attempting to compile and run the program?

a. Prints: 0,0,1,0,0,1
b. Prints: 0,1,0,0,0,0
c. Prints: 0,1,1,0,0,1
d. Prints: 0,1,0,0,0,1
e. Prints: 1,1,1,0,0,1
f. Compiler Error
g. Run Time Error
h. None of the Above

Question 11


class Level1Exception extends Exception {}
class Level2Exception extends Level1Exception {}
class Level3Exception extends Level2Exception {}
class Brown {
  public static void main(String args[]) {
    int a, b, c, d, f;
    a = b = c = d = f = 0;
    int x = 4;

    try {
      switch (x) {
        case 1: throw new Level1Exception();
        case 2: throw new Level2Exception();
        case 3: throw new Level3Exception();
      }
      a++;
    }
    catch (Level3Exception e) { b++;}
    catch (Level2Exception e) { c++;}
    catch (Level1Exception e) { d++;}
    finally {f++;}
    System.out.print(a+","+b+","+c+","+d+","+f);
  }
}

What is the result of attempting to compile and run the program?

a. Prints: 0,0,0,1,1
b. Prints: 0,0,1,1,1
c. Prints: 0,1,1,1,1
d. Prints: 1,1,1,1,1
e. Prints: 0,0,1,0,1
f. Prints: 0,1,0,0,1
g. Prints: 1,0,0,0,1
h. Compiler Error
i. Run Time Error
j. None of the Above

Question 12


class A {
  public static void main(String[] args) {
    boolean b;
    if (b = false) {
      System.out.print("A");
    } else if (b) {
      System.out.print("B");
    } else if (!b) {
      System.out.print("C");
    } else {
      System.out.print("D");
    }
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: A
b. Prints: B
c. Prints: C
d. Prints: D
e. Prints: E
f. Runtime Exception
g. Compiler Error
h. None of the Above

Question 13


class Purple {
  public static void main(String args[]) {
    int x = 6;
    int success = 0;
    do  {
      switch(x) {
        case 0: System.out.print("0");
          x += 5; break;
        case 1: System.out.print("1");
          x += 3; break;
        case 2: System.out.print("2");
          x += 1; break;
        case 3: System.out.print("3");
          success++; break;
        case 4: System.out.print("4");
          x -= 1; break;
        case 5: System.out.print("5");
          x -= 4; break;
        case 6: System.out.print("6");
          x -= 5; break;
        default: System.out.print("Default ");
      }
    } while ((x != 3) || (success < 2));
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: 0123456
b. Prints: 60514233
c. Prints: 6152433
d. Prints: 61433
e. Prints: 6143
f. Prints: 6
g. Prints: Default
h. Runtime Exception
i. Compiler Error
j. None of the Above

Question 14


class Level1Exception extends Exception {}
class Level2Exception extends Level1Exception {}
class Level3Exception extends Level2Exception {}
class Purple {
  public static void main(String args[]) {
    int a,b,c,d,f,g,x;
    a = b = c = d = f = g = 0;
    x = 1;
    try {
      throw new Level1Exception();
      try {
        switch (x) {
          case 1: throw new Level1Exception();
          case 2: throw new Level2Exception();
          case 3: throw new Level3Exception();
        }
        a++;
      }
      catch (Level2Exception e) {b++;}
      finally{c++;}
    }
    catch (Level1Exception e) { d++;}
    catch (Exception e) {f++;}
    finally {g++;}
    System.out.print(a+","+b+","+c+","+d+","+f+","+g);
  }
}

What is the result of attempting to compile and run the program?

a. Prints: 1,1,1,0,0,1
b. Prints: 0,1,1,0,0,1
c. Prints: 0,1,0,0,0,0
d. Prints: 0,1,0,0,0,1
e. Prints: 0,0,1,0,0,1
f. Compiler Error
g. Run Time Error
h. None of the Above

Question 15


class E {
  public static void main (String[] args) {
    int i = 0, j = 0;
    while (i++ < 3) do
      System.out.print(j);
    while (j++ < 3);
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: 0001
b. Prints: 001122
c. Prints: 012012
d. Prints: 012345
e. Prints: 1112
f. Prints: 111222
g. Prints: 121212
h. Runtime Exception
i. Compiler Error
j. None of the Above

Question 16


class H {
  public static void main (String[] args) {
    int i = 0, j = 9;
    while (i++ <= j--) {
      i++;
      if (j < 5) break;
    }
    System.out.print(i + "," + j);
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: 4,7
b. Prints: 6,6
c. Prints: 7,2
d. Prints: 8,5
e. Prints: 9,4
f. Runtime Exception
g. Compiler Error
h. None of the Above

Question 17


class D {
  public static void main (String[] args) {
    int i = 0, j = 0, k = 0;
    do while (i++ < 3)
      System.out.print(k++);
    while (j++ < 3);
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: 0001
b. Prints: 012
c. Prints: 012012
d. Prints: 012345
e. Prints: 001122
f. Prints: 1112
g. Prints: 111222
h. Prints: 121212
i. Runtime Exception
j. Compiler Error
k. None of the Above

Question 18


class A {
  public static void main (String args[]) {
    int i = 0, j = 0, k = 0;
label1:
    for (;;) {
      i++;
label2:
      do {
        k = i + j;
        switch (k) {
        case 0: continue label2;
          case 1: continue label1;
          case 2: break;
          case 3: break label2;
          case 4: break label1;
          default: break label1;
        }
      } while (++j<5);
    }
    System.out.println(i + "," + j);
  }
}


What is the result of attempting to compile and run the above program?

a. Prints: 2,1
b. Prints: 2,2
c. Prints: 3,1
d. Prints: 3,2
e. Prints: 3,3
f. Runtime Exception
g. Compiler Error
h. None of the Above

Question 19


class A {
  public static void main (String args[]) {
    int h = 0, i = 0, j = 0, k = 0;
label1:
    for (;;) {
      h++;
label2:
      do {
        i++;
        k = h + i + j;
        switch (k) {
          default: break label1;
          case 1: continue label1;
          case 2: break;
          case 3: break label2;
          case 4: continue label2;
          case 5: continue label1;
        }
      } while (++j<5);
    }
    System.out.println(h + "," + i + "," + j);
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: 1,2,3
b. Prints: 1,3,2
c. Prints: 2,2,2
d. Prints: 2,4,1
e. Prints: 2,4,2
f. Runtime Exception
g. Compiler Error
h. None of the Above


Copyright © 2002, Dan Chisholm
All rights reserved.
1